{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run a standalone AssistantAgent\n",
    "\n",
    "AG2 supports running `AssistantAgent` as a standalone agent with the ability to execute simple tasks without the need for interacting with other agents.\n",
    "\n",
    "In this notebook, we'll enable our assistant agent access to surf the web. We will use the `BrowserUseTool` Tool, for which we need to install the browser-use optional dependency and [playwright](https://playwright.dev/python/docs/intro).\n",
    "\n",
    "\\:\\:\\:info Requirements\n",
    "\n",
    "**Warning:**\n",
    "[`Browser Use`](https://github.com/browser-use/browser-use) requires **Python 3.11 or higher**.\n",
    "\n",
    "Install `ag2` and `playwright`:\n",
    "```bash\n",
    "pip install -U ag2[browser-use]\n",
    "# Installs Playwright and browsers for all OS\n",
    "playwright install\n",
    "# Additional command, mandatory for Linux only\n",
    "playwright install-deps\n",
    "```\n",
    "\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "> ```bash\n",
    "> pip install -U autogen[openai,browser-use]\n",
    "> ```\n",
    "> or  \n",
    "> ```bash\n",
    "> pip install -U ag2[browser-use]\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "\n",
    "\n",
    "\\:\\:\\:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import autogen\n",
    "from autogen import AssistantAgent\n",
    "from autogen.tools.experimental.browser_use.browser_use import BrowserUseTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set your API Endpoint\n",
    "\n",
    "The [`LLMConfig.from_json`](https://docs.ag2.ai/latest/docs/api-reference/autogen/config_list_from_json/#autogen.config_list_from_json) method loads a list of configurations from an environment variable or a JSON file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_config = autogen.LLMConfig.from_json(path=\"OAI_CONFIG_LIST\", temperature=0.8, timeout=600).where(tags=\"gpt-4.1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Configure your assistant agent\n",
    "\n",
    "Here we will configure two assistant agents:\n",
    "1. x_assistant, tasked with exploring the trending topics on X (formally Twitter)\n",
    "2. arxiv_researcher, tasked with discovery of papers that align with the hot topic of the day\n",
    "\n",
    "We will set the browser configuration to not run headless, this will open the browser as a window so you can see it in action."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_assistant = AssistantAgent(name=\"x_assistant\", llm_config=llm_config)\n",
    "\n",
    "arxiv_researcher = AssistantAgent(name=\"arxiv\", llm_config=llm_config)\n",
    "\n",
    "browser_use_tool = BrowserUseTool(\n",
    "    llm_config=llm_config,\n",
    "    browser_config={\"headless\": False},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running the assistant agents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's run our x_assistant to discover the hot topic of the day.\n",
    "\n",
    "To be able to do this we give our assistant the capability to browse the web using a `BrowserUseTool` Tool."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hot_topic_res = x_assistant.run(\n",
    "    \"Find out today's hot topic and an influencer who is talking about it on X using a web search\",\n",
    "    tools=browser_use_tool,\n",
    "    user_input=False,\n",
    ")\n",
    "\n",
    "print(hot_topic_res.summary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After discovering the hot topic, let's run the discovery of papers that align with the topic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "paper_abstract = arxiv_researcher.run(\n",
    "    \"Get the abstract of a relevant paper based on:\\n\" + hot_topic_res.summary,\n",
    "    user_input=False,\n",
    ")\n",
    "\n",
    "print(paper_abstract.summary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's create an X post using our x_assistant"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Scenario 1. This task requires x_assistant's past state\n",
    "run_result = x_assistant.run(\n",
    "    message=\"Create an X post based on the hot topic and the following and mention the influencer:\\n\"\n",
    "    + paper_abstract.summary,\n",
    "    user_input=False,\n",
    ")\n",
    "run_result.process()\n",
    "print(run_result.summary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, let's ask our x_assistant who should we follow on X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Scenario 2. Doing another task that does not require history or past state\n",
    "\n",
    "run_result = x_assistant.run(\n",
    "    \"Find a influencer I should follow on Twitter by searching the web\",\n",
    "    clear_history=True,\n",
    "    tools=browser_use_tool,\n",
    "    user_input=False,\n",
    ")\n",
    "run_result.process()\n",
    "print(run_result.summary)"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Run a standalone AssistantAgent, browsing the web using the BrowserUseTool",
   "tags": [
    "assistantagent",
    "run",
    "browser-use",
    "webscraping",
    "function calling"
   ]
  },
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
